package com.livestocksolutions.ui;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.border.MatteBorder;

import com.livestocksolutions.Bovine;
import com.livestocksolutions.FeedHistory;
import com.livestocksolutions.LivestockSolutions;
import com.livestocksolutions.MedicalHistory;
import com.livestocksolutions.MedicineHistory;
import com.livestocksolutions.util.JDatePickerDate;
import com.livestocksolutions.util.Table;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;

/**
 * 
 * @author Matt Lang
 *
 */
public class ExpenditurePanel extends JPanel {

	/**
	 * ID for serialization
	 */
	private static final long serialVersionUID = -2673246903622233481L;
	protected JPanel displayPanel = new JPanel();
	protected final JDatePickerDate dateStart = JDatePickerDate.createJDatePicker();
	protected final JDatePickerDate dateEnd = JDatePickerDate.createJDatePicker();
	
	public ExpenditurePanel() 
	{
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		
		JLabel lblIncomePanel = new JLabel("Expenditure Panel");
		lblIncomePanel.setFont(new Font("Tahoma", Font.BOLD, 20));
		panel.add(lblIncomePanel);
		
		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.NORTH);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.X_AXIS));
		
		JButton btnGenerateReport = new JButton("Generate Report");
		panel_2.add(btnGenerateReport);
		
		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3, BorderLayout.CENTER);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		
		panel_3.add(displayPanel, BorderLayout.CENTER);
		
		
		panel_2.add(new JLabel("Start Date:"));
		panel_2.add(dateStart);
		panel_2.add(new JLabel("End Date:"));
		panel_2.add(dateEnd);
		
		btnGenerateReport.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(dateStart.getDate()==null ||dateEnd.getDate()==null)
					outputDateErrorMessage();
				else
					updatePanel(dateStart.getDate(), dateEnd.getDate());
			}
		});
	}

	public void outputDateErrorMessage()
	{
		JOptionPane.showMessageDialog(MainFrame.getInstance(),"Please enter both start and end date","Invalid Dates", JOptionPane.OK_OPTION);
	}

	public void updatePanel(Date startDate, Date endDate)
	{
		ArrayList<Bovine> bovines = new ArrayList<Bovine>(Arrays.asList(LivestockSolutions.getBovines()));
		NumberFormat formatter = NumberFormat.getCurrencyInstance();
		Iterator<Bovine> i = bovines.iterator();
		Table updatePanel = new Table("Expenses on cattle between "+JDatePickerDate.format(startDate)+" and "+JDatePickerDate.format(endDate), true,
				"Visual ID", "Feed Expenses","Medicine Expenses", "Vet Expenses", "Total");
		updatePanel.setHeaderClickable(0, false);
		updatePanel.setHeaderClickable(1, false);
		updatePanel.setHeaderClickable(2, false);
		updatePanel.setHeaderClickable(3, false);
		updatePanel.setHeaderClickable(4, false);
		double totalMedical, totalFeed, totalMedicine;
		totalMedical = totalFeed = totalMedicine = 0;
		
		while(i.hasNext())
		{
			Bovine b = i.next();
			double  medicalExpenses, medicineExpenses, feedExpenses;
			medicalExpenses = medicineExpenses = feedExpenses = 0;
			MedicalHistory[] medicals = b.getMedicals();
			FeedHistory[] feeds = b.getHandFeeds();
			MedicineHistory[] medicines = b.getMedicines();
			for(int i1=0;i1<medicals.length;i1++)
			{
				if(medicals[i1].getDate().after(startDate)&&medicals[i1].getDate().before(endDate))
					medicalExpenses += medicals[i1].getCost();
			}
			for(int i1=0;i1<feeds.length;i1++)
			{
				if(feeds[i1].getDate().after(startDate)&&feeds[i1].getDate().before(endDate))
					feedExpenses += feeds[i1].getCost();
			}
			for(int i1=0;i1<medicines.length;i1++)
			{
				if(medicines[i1].getDate().after(startDate)&&medicines[i1].getDate().before(endDate))
					medicineExpenses += medicines[i1].getCost();
			}
			totalFeed += feedExpenses;
			totalMedical += medicalExpenses;
			totalMedicine += medicineExpenses;
			
			if(feedExpenses+medicalExpenses+medicineExpenses!=0)
			{
				updatePanel.addRow(new JLabel(b.getEartag().getEartagID()), new JLabel(String.valueOf(formatter.format(feedExpenses))),
						new JLabel(String.valueOf(formatter.format(medicineExpenses))), new JLabel(String.valueOf(formatter.format(medicalExpenses))),
						new JLabel(String.valueOf(formatter.format(medicalExpenses+medicineExpenses+feedExpenses))));
				
				final long id = b.getID();
				updatePanel.addMouseListener(new MouseListener() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						if(MainFrame.getInstance().showBovine(id)) {
						}
					}
					@Override public void mouseEntered(MouseEvent arg0) {}
					@Override public void mouseExited(MouseEvent arg0) {}
					@Override public void mousePressed(MouseEvent arg0) {}
					@Override public void mouseReleased(MouseEvent arg0) {}
				});
			}
		}
			
		if(updatePanel.getNumRows() != 0)
		{
			updatePanel.addRow(new JLabel("Totals"), new JLabel(String.valueOf(formatter.format(totalFeed))),
					new JLabel(String.valueOf(formatter.format(totalMedicine))), new JLabel(String.valueOf(formatter.format(totalMedical))), 
					new JLabel(String.valueOf(formatter.format(totalMedical+totalMedicine+totalFeed))));
			updatePanel.setRowClickable(false);
			updatePanel.setRowBorder(new MatteBorder(1,0,0,0,Color.black));
		}
		updatePanel.setEmptyMessage("No Cattle sold within the given period");
		displayPanel.add(updatePanel);	
	}
}
